package com.example.inter.tourdemo.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.inter.tourdemo.R;
import com.example.inter.tourdemo.adapter.MeAdapterGrid;
import com.example.inter.tourdemo.adapter.MeAdapterLinear;
import com.example.inter.tourdemo.bean.MeBean;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by Inter巩海亮 on 2017-03-28.
 * 微信:
 * QQ号:
 * 邮箱:dahaizhiliang@126.com
 */

public class MeFragment extends SupportFragment {

    //九宫格文字
    private static String[] name_gird = new String[]{"订单", "待支付", "待出行", "待点评"
            , "优惠券", "积分", "钱包", "积分"};
    @Bind(R.id.grid_container)
    RecyclerView mGridContainer;
    @Bind(R.id.linear_container)
    RecyclerView mLinearContainer;
    //九宫格图片
    private int[] icons = new int[]{R.drawable.me_integral, R.drawable.me_wallet, R.drawable.me_integral, R.drawable.me_integral
            , R.drawable.me_wallet, R.drawable.me_integral, R.drawable.me_wallet, R.drawable.me_integral};
    //垂直列表
    private static String[] name_linear = new String[]{"我的订单", "我的收藏", "我的游记", "我的点评"
            , "优惠券", "我的积分", "旅游顾问"};

    //数据
    private ArrayList<MeBean> mGridDataList;
    private ArrayList mLinearDataList;

    private MeAdapterGrid mMeAdapterGrid;
    private MeAdapterLinear mMeAdapterLinear;

    public static MeFragment newInstance() {

        Bundle args = new Bundle();
        MeFragment fragment = new MeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View meView = inflater.inflate(R.layout.fragment_me, container, false);
        ButterKnife.bind(this, meView);

        initData();
        return meView;
    }

    public void initData() {
        mGridDataList = new ArrayList<>();
        mLinearDataList = new ArrayList<>();
        //九宫格 文字Ions
        for (int i = 0; i < name_gird.length; i++) {
            MeBean meBean = new MeBean();
            meBean.functionIcon = icons[i];
            meBean.functionName = name_gird[i];
            mGridDataList.add(meBean);
        }

        //垂直列表
        for (int j = 0; j < name_linear.length; j++) {
            mLinearDataList.add(name_linear[j]);
        }
        initAdapter();
    }

    private void initAdapter() {
        //九宫格适配器
        mMeAdapterGrid = new MeAdapterGrid(R.layout.me_item_grid, mGridDataList);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        mGridContainer.setLayoutManager(gridLayoutManager);
        mGridContainer.setAdapter(mMeAdapterGrid);
        mGridContainer.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(getContext(), "点击了条目--" + position, Toast.LENGTH_SHORT).show();

            }
        });
        //垂直列表适配器
        mMeAdapterLinear = new MeAdapterLinear(R.layout.me_item_linear, mLinearDataList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mLinearContainer.setNestedScrollingEnabled(false);
        mLinearContainer.setLayoutManager(linearLayoutManager);
        mLinearContainer.setAdapter(mMeAdapterLinear);
        mLinearContainer.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(getContext(), "点击了条目 " + mLinearDataList.get(position), Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
